import { CoolLocalStorage } from 'src/app/common/coolStorage/cool-local-storage';
import { ActivatedRoute } from '@angular/router';
import { Toast } from 'ng-zorro-antd-mobile';
import { DetailService } from './../estate-detail/services/detail.service';
import { Component, OnInit } from '@angular/core'
import { Location } from '@angular/common'
import { log } from 'util';

@Component({
    selector: 'app-estate-images',
    templateUrl: './estate-images.component.html',
    styleUrls: ['./estate-images.component.scss']
})
export class EstateImagesComponent implements OnInit {

    public Photos: any = [];

    public loading = false;
    public viewImgs:any = [];
    public iShowVI:boolean = false;

    public showImgActive:any;

    public imgview={
        "show":false,
        "list":[],
        "index":0
    }

    public sliderConfig = {
        effect: 'slide',
        autoHeight: true,
        loop: true
      }

    tab = ['楼盘图', '户型图']
    constructor(
        private location: Location,
        private detailService: DetailService,
        private routeInfo: ActivatedRoute,
        private coolLocalStorage: CoolLocalStorage,
        private _toast: Toast
    ) { }

    ngOnInit() {
        let id = this.routeInfo.snapshot.queryParams['project'];
        this.getEstateDetail(id)
    }

    handleClose(){
        this.imgview.show = false;
    }

    onTabChange(o) {
        this.scrollSpeed(o.index);
    }
    showViewModal(index,items){

        console.log(index);
        

      items.forEach(element => {
          element['Photo'] = element.src;
      });

      this.imgview = {
        "show":true,
        "list":items,
        "index":index
      }
    }

    scrollSpeed(index) {

        let viewHeight = window.document.documentElement.clientHeight
        let clientHeight = window.document.body.clientHeight
        let number = clientHeight - viewHeight;

        let speed = number / 500;
        speed = speed < 1 ? 10 : speed;

        if (index === 0) {
            let scrollToTop = window.setInterval(() => {
                let pos = window.pageYOffset;
                if (pos > 0) {
                    window.scrollTo(0, pos - 10);
                } else {
                    window.clearInterval(scrollToTop);
                }

            }, speed);

        } else {

            let scrollToTop2 = window.setInterval(() => {

                let pos = window.pageYOffset;

                if (pos < number) {
                    window.scrollTo(0, pos + 10);
                } else {
                    window.clearInterval(scrollToTop2);
                }
            }, speed);
        }
    }

    onClickLeft() {
        this.location.back()
    }

    getEstateDetail(id) {
        this.loading = true
        let reqdata = {
            project: id,
            zone:this.coolLocalStorage.getItem('zone')

        }

        this.detailService.getEstateDetail(reqdata, res => {

            if (res.Success && res.Result) {
                let items = res.Result;
                let photo = JSON.parse(items.Photo);

                //相册
                photo.forEach(element => {
                    if (element.Category) {
                        element.Items.forEach(el => {
                            let o = {
                                src: el,
                                Category: element.Category
                            }
                            this.Photos.push(o);
                        });
                    }
                });

                //户型图
                items.LayoutSummarys.forEach(element => {

                    let _photo = JSON.parse(element.Photo);

                    _photo.forEach(elm => {
                        if (elm.Category) {
                            elm.Items.forEach(el => {
                                let o = {
                                    src: el,
                                    Category: elm.Category
                                }
                                this.Photos.push(o);

                            });
                        }
                    });
                });

                this.viewImgs = this.Photos;
                this.Photos = this.formatList(this.Photos);
                this.loading = false;

            }

        })
    }

    closeImgView(){
        this.iShowVI = false;
    }


    private formatList(Photos) {

        let items = [];
        for (let i = 0; i < Photos.length; i++) {

            let _p: any = {},
                element = Photos[i];

            if (i != 0) {
                _p = Photos[i - 1];
            }

            if (element['Category'] != _p['Category']) {
                let _o = {};
                _o['Category'] = element['Category'];
                _o['Childs'] = [];
                items.push(_o);
            }
        }

        for (let k = 0; k < Photos.length; k++) {
            let _o = Photos[k]
            items.forEach(element => {
                if (_o['Category'] === element['Category']) {
                    let item= {
                        src:_o['src'],
                        index:k
                    }
                    element.Childs.push(item);
                }
            });
        }
        return items;
    }

}
